﻿/*******************************************************************************************
 * Copyright (c) 2006-2009 Alex Robson, Rob Simmons, Craig Israel, and Rachel Twyford
 *
 * Use of this software in any form requires that you have both 
 * read and agreed to the following terms:
 *
 * Permission is hereby granted, free of charge, to any person obtaining
 * a copy of this software and associated documentation files (the "Software"), 
 * to deal in the Software without restriction, including without limitation 
 * the rights to use, copy, modify, merge, publish, distribute, sublicense, 
 * and/or sell copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following conditions: 
 *
 * Redistributions of source code must retain the above copyright
 * notice, this list of conditions and the following disclaimer.
 * Redistributions in binary form must reproduce the above copyright notice, 
 * this list of conditions and the following disclaimer in the 
 * documentation and/or other materials provided with the distribution.
 * Neither the name of Nvigorate nor the names of its contributors may 
 * be used to endorse or promote products derived from this software without 
 * specific prior written permission. 
 *
 * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY
 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 
 * DISCLAIMED. IN NO EVENT SHALL ANY CONTRIBUTOR BE LIABLE FOR ANY
 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 * ****************************************************************************************/

using System;
using System.Configuration;
using Nvigorate.Common.Reflection;

namespace Nvigorate.Relational.Mapping.Index
{
    public class MapConfigurationSection : ConfigurationSection
    {
        [ConfigurationProperty("bindingSources")]
        public BindingMapCollection BindingElements
        {
            get { return ((BindingMapCollection)(base["bindingSources"])); }
            internal set { base["bindingSources"] = value;}
        }

        [ConfigurationProperty("relationalSources")]
        public RelationalMapCollection RelationalElements
        {
            get { return ((RelationalMapCollection)(base["relationalSources"])); }
        }

        [ConfigurationProperty("scanTargets")]
        public ScanCollection MapScanCollection
        {
            get { return ((ScanCollection) (base["scanTargets"])); }
            internal set { base["scanTargets"] = value;}
        }

        [ConfigurationProperty("enableCache", DefaultValue = false, IsKey = false, IsRequired = false)]
        public bool CachingEnabled
        {
            get { return (bool)base["enableCache"]; }
            set { base["enableCache"] = value; }
        }

        [ConfigurationProperty("cacheDirectory", DefaultValue = "", IsKey=false, IsRequired = false)]
        public string CacheDirectory
        {
            get { return (string) base["cacheDirectory"]; }
            set { base["cacheDirectory"] = value; }
        }

        [ConfigurationProperty("expiresIn", DefaultValue = 24, IsKey = false, IsRequired = false)]
        public int Expiration
        {
            get { return int.Parse(base["expiresIn"].ToString()); }
            set { base["expiresIn"] = value; }
        }
    }

    [ConfigurationCollection(typeof(ScanTarget), AddItemName = "scan")]
    public class ScanCollection : ConfigurationElementCollection
    {
        protected override ConfigurationElement CreateNewElement()
        {
            return new ScanTarget();
        }

        protected override object GetElementKey(ConfigurationElement element)
        {
            return ((ScanTarget)(element)).Name;
        }

        public ScanTarget this[int idx]
        {
            get
            {
                return (ScanTarget)BaseGet(idx);
            }
        }

        internal void Add(ScanTarget scanTarget)
        {
            BaseAdd(scanTarget);
        }
    }

    [ConfigurationCollection(typeof(MapElement), AddItemName="binding")]
    public class BindingMapCollection : ConfigurationElementCollection
    {
        protected override ConfigurationElement CreateNewElement()
        {
            return new MapElement();
        }
        
        protected override object GetElementKey(ConfigurationElement element)
        {
            return ((MapElement)(element)).Name;
        }
        
        public MapElement this[int idx]
        {
            get
            {
                return (MapElement)BaseGet(idx);    
            }
        }

        internal void Add(MapElement element)
        {
            BaseAdd(element);
        }
    }

    [ConfigurationCollection(typeof(MapElement), AddItemName = "relational")]
    public class RelationalMapCollection : ConfigurationElementCollection
    {
        protected override ConfigurationElement CreateNewElement()
        {
            return new MapElement();
        }

        protected override object GetElementKey(ConfigurationElement element)
        {
            return ((MapElement)(element)).Name;
        }

        public MapElement this[int idx]
        {
            get
            {
                return (MapElement)BaseGet(idx);
            }
        }

    }

    public class MapElement : ConfigurationElement
    {
        public Type MappedType
        {
            get
            {
                return Reflector.GetType(Type);
            }
        }

        private Guid _id = Guid.NewGuid();

        [ConfigurationProperty("name", DefaultValue = "", IsKey = false, IsRequired = false)]
        public string Name
        {
            get
            {
                return _id.ToString();
            }
        }

        [ConfigurationProperty("source", DefaultValue = "class", IsKey = false, IsRequired = false)]
        public string MapSource
        {
            get
            {
                return ((string)(base["source"]));
            }
            set
            {
                base["source"] = value;
            }
        }

        [ConfigurationProperty("mapSet", DefaultValue = "", IsKey = false, IsRequired=false)]
        public string MapSet
        {
            get
            {
                return ((string) (base["mapSet"]));
            }
            set
            {
                base["mapSet"] = value;
            }
        }

        [ConfigurationProperty("type", DefaultValue = "", IsKey = false, IsRequired = true)]
        public string Type
        {
            get
            {
                return (string)(base["type"]);
            }
            set
            {
                base["type"] = value;
            }
        }

        [ConfigurationProperty("xmlPath", DefaultValue = "", IsKey = false, IsRequired = false)]
        public string XmlPath
        {
            get
            {
                return ((string)(base["xmlPath"]));
            }
            set
            {
                base["xmlPath"] = value;
            }
        }
        
    }

    public class ScanTarget : ConfigurationElement
    {
        private Guid _id = Guid.NewGuid();
        
        [ConfigurationProperty("name", DefaultValue="", IsKey=false, IsRequired = false)]
        public string Name
        {
            get
            {
                return _id.ToString();
            }
        }

        [ConfigurationProperty("directory", DefaultValue = "", IsRequired = false)]
        public string MapDirectory
        {
            get
            {
                return ((string) (base["directory"]));
            }
            set
            {
                base["directory"] = value;
            }
        }

        [ConfigurationProperty("bindingExtension", DefaultValue = "binding", IsRequired = false)]
        public string BindingMapExtension
        {
            get
            {
                return ((string) base["bindingExtension"]);
            }
            set
            {
                base["bindingExtension"] = value;
            }
        }

        [ConfigurationProperty("relationalExtension", DefaultValue = "relational", IsRequired = false)]
        public string RelationalMapExtension
        {
            get
            {
                return ((string)base["relationalExtension"]);
            }
            set
            {
                base["relationalExtension"] = value;
            }
        }

        [ConfigurationProperty("mapSet", DefaultValue = "", IsKey = false, IsRequired = false)]
        public string MapSet
        {
            get
            {
                return ((string)(base["mapSet"]));
            }
            set
            {
                base["mapSet"] = value;
            }
        }

        [ConfigurationProperty("namespace", DefaultValue = "", IsRequired = false)]
        public string Namespace
        {
            get
            {
                return ((string)(base["namespace"]));
            }
            set
            {
                base["namespace"] = value;
            }
        }
    
        [ConfigurationProperty("relativeTo", DefaultValue="None", IsRequired=false)]
        public StandardPaths RelativeTo
        {
            get { return (StandardPaths) Enum.Parse(typeof(StandardPaths), base["relativeTo"].ToString());}
            set { base["relativeTo"] = value.ToString(); }
        }

        public bool IsRelative
        {
            get { return base["relativeTo"].ToString() != "None"; }
        }
    }
}